package com.yashi.appController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.yashi.entity.TbDepartment;
import com.yashi.service.DepartmentService;

@RestController
@RequestMapping("/appDept")
public class AppDepartmentController {
	
	@Resource
	private DepartmentService departmentService;
	
	/**
	 * 查询所有部门
	 * @param request
	 * @return
	 */
	@RequestMapping("/findDeptList")
	public Map<String, Object> findDeptList(HttpServletRequest request){
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			List<TbDepartment> findDeptList = departmentService.findDeptList();
			request.setAttribute("findDeptList", findDeptList);
			map.put("status", "1");
			map.put("message","查询部门成功");
			map.put("data", findDeptList);
			return map;
		} catch (Exception e) {
			map.put("status", "0");
			map.put("message","查询部门失败");
			return map;
		}
	}
	
	/**
	 * 模糊查询
	 * @param deptName
	 * @param request
	 * @return
	 */
	@RequestMapping("/SelectDeptByName")
	public Map<String, Object> SelectDeptByName(@RequestBody TbDepartment tbDept,HttpServletRequest request){
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			List<TbDepartment> findDeptList = departmentService.selectDeptByName1(tbDept);
			request.setAttribute("findDeptList", findDeptList);
			map.put("status", "1");
			map.put("message","查询部门成功");
			map.put("data", findDeptList);
			return map;
		} catch (Exception e) {
			map.put("status", "0");
			map.put("message","查询部门失败");
			return map;
		}
	}
	
	/**
	 * 为修改查询部门信息
	 * @param deptId
	 * @param request
	 * @return
	 */
	@RequestMapping("/findDeptById")
	public String findDeptById(String deptId,HttpServletRequest request){
		try {
			TbDepartment dept = departmentService.findDeptById(deptId);
			request.setAttribute("dept", dept);
			return "updateDept";
		} catch (Exception e) {
			return e.getMessage();
		}
	}

	
}
